import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
       //m1();
       //m2();
       //m3();
        //m4
        m5();


    }
    private static void m1(){
        //jdbc操作数据库

        Connection root =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;

        try {
            //jdbc
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            root = DriverManager.getConnection(url, "root", "5011");
/*
            if (root != null){
                System.out.println("连接成功");
            }
*/
            //3.写sql
            String sql="select *from user";
            //4。预编译sql root.prepareStatement
            preparedStatement = root.prepareStatement(sql);
            //5.执行sql preparedStatement.executeQuery()
            resultSet = preparedStatement.executeQuery();//ResultSet是结果集
            //6.遍历结果
            while (resultSet.next()) {
                //列
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"___"+name+"___"+pass);
            }
/*
            //7关闭资源
            //关闭结果集
            resultSet.close();
            //关闭预编译
            preparedStatement.close();
            //关闭连接
            root.close();
*/

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //7关闭资源
            try {
                if (resultSet!=null){
                    resultSet.close();
                }
                if (preparedStatement!=null)
                    preparedStatement.close();
                if (root!=null)
                    root.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }
    private static void m2(){

        //jdbc操作数据库
        //键盘录入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的姓名");
        String name = scanner.next();

        Connection root =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;

        try {
            //jdbc
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            root = DriverManager.getConnection(url, "root", "5011");
/*
            if (root != null){
                System.out.println("连接成功");
            }
*/
            //3.写sql
            String sql="select *from user where name like ?";
            //4。预编译sql root.prepareStatement
            preparedStatement = root.prepareStatement(sql);
            preparedStatement.setString(1,"%"+name+"%");

            //5.执行sql preparedStatement.executeQuery()
            resultSet = preparedStatement.executeQuery();//ResultSet是结果集
            //6.遍历结果
            while (resultSet.next()) {
                //列
                int id = resultSet.getInt(1);
                String userName = resultSet.getString(2);
                String pass = resultSet.getString(3);
                System.out.println(id+"___"+userName+"___"+pass);
            }
/*
            //7关闭资源
            //关闭结果集
            resultSet.close();
            //关闭预编译
            preparedStatement.close();
            //关闭连接
            root.close();
*/

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //7关闭资源
            try {
                if (resultSet!=null){
                    resultSet.close();
                }
                if (preparedStatement!=null)
                    preparedStatement.close();
                if (root!=null)
                    root.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }
    //新增
    private static void m3(){
        //jdbc操作数据库

        Connection root =null;
        PreparedStatement preparedStatement =null;
       // ResultSet resultSet =null;

        try {
            //jdbc
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            root = DriverManager.getConnection(url, "root", "5011");
/*
            if (root != null){
                System.out.println("连接成功");
            }
*/
            //3.写sql
            String sql="insert into user(name,pass) values (?,?)";
            //4。预编译sql root.prepareStatement
            preparedStatement = root.prepareStatement(sql);
            //5.给占位符赋值？
            preparedStatement.setString(1,"小明");
            preparedStatement.setString(2,"123456");
            //5.执行sql preparedStatement.executeQuery()
            //resultSet = preparedStatement.executeQuery();//ResultSet是结果集
           int result = preparedStatement.executeUpdate();//执行增加


if (result>0){
    System.out.println("插入成功");
}else {
    System.out.println("插入失败");
}

            /*
            //7关闭资源

            //关闭预编译
            preparedStatement.close();
            //关闭连接
            root.close();
*/

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //7关闭资源
            try {

                if (preparedStatement!=null)
                    preparedStatement.close();
                if (root!=null)
                    root.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }
    //删除
    private static void m5(){
        //jdbc操作数据库

        Connection root =null;
        PreparedStatement preparedStatement =null;
        // ResultSet resultSet =null;

        try {
            //jdbc
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            root = DriverManager.getConnection(url, "root", "5011");
/*
            if (root != null){
                System.out.println("连接成功");
            }
*/
            //3.写sql
            String sql="delete from user where id =?";
            //4。预编译sql root.prepareStatement
            preparedStatement = root.prepareStatement(sql);
            //5.给占位符赋值？

            preparedStatement.setObject(1,1);

            //5.执行sql preparedStatement.executeQuery()
            //resultSet = preparedStatement.executeQuery();//ResultSet是结果集
            int result = preparedStatement.executeUpdate();//执行增加


            if (result>0){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }

            /*
            //7关闭资源

            //关闭预编译
            preparedStatement.close();
            //关闭连接
            root.close();
*/

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //7关闭资源
            try {

                if (preparedStatement!=null)
                    preparedStatement.close();
                if (root!=null)
                    root.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }
    //修改
    private static void m4(){
        //jdbc操作数据库

        Connection root =null;
        PreparedStatement preparedStatement =null;
        // ResultSet resultSet =null;

        try {
            //jdbc
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取链接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            root = DriverManager.getConnection(url, "root", "5011");
/*
            if (root != null){
                System.out.println("连接成功");
            }
*/
            //3.写sql
            String sql="update user set name=?,pass=? where id=?";
            //4。预编译sql root.prepareStatement
            preparedStatement = root.prepareStatement(sql);
            //5.给占位符赋值？
            preparedStatement.setString(1,"蛋黄");
            preparedStatement.setString(2,"888");
            preparedStatement.setInt(3,3);
            //5.执行sql preparedStatement.executeQuery()
            //resultSet = preparedStatement.executeQuery();//ResultSet是结果集
            int result = preparedStatement.executeUpdate();//执行增加


            if (result>0){
                System.out.println("更新成功");
            }else {
                System.out.println("更新失败");
            }

            /*
            //7关闭资源

            //关闭预编译
            preparedStatement.close();
            //关闭连接
            root.close();
*/

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //7关闭资源
            try {

                if (preparedStatement!=null)
                    preparedStatement.close();
                if (root!=null)
                    root.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }






    }

